package com.app.controller.backstage;

import cn.hutool.core.util.StrUtil;
import com.app.common.Result;
import com.app.event.ComplaintEvent;
import com.app.model.AdminUserModel;
import com.app.model.ComplaintModel;
import com.app.model.UserModel;
import com.app.service.AdminUserService;
import com.app.service.ComplaintService;
import com.app.service.UserService;
import com.app.util.TokenUtils;
import com.baomidou.mybatisplus.annotation.TableField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Controller
@RestController
@RequestMapping("/behind")
public class AdminComplaintController {
    @Autowired
    private ComplaintService complaintService;
    @Autowired
    private UserService userService;
    @Autowired
    private AdminUserService adminUserService;
    @Autowired
    private ApplicationContext applicationContext;
    //我要投诉
    @PostMapping("/addComplaint")
    public Result<Object> addComplaint(@RequestBody ComplaintModel model, HttpServletRequest request) {
        String s = complaintService.addComplaint(model);
        ComplaintModel complaintModel = complaintService.getById(model.getId());
        applicationContext.publishEvent(new ComplaintEvent(complaintModel));
        return Result.success(s);
    }

    //投诉列表
    @GetMapping("/selectComplaintList")
    public Result<Object> selectComplaintList(HttpServletRequest request, Integer page, Integer limit, String keyWord, String selectCrtTime, String isJiuquan) {
        if (StrUtil.isNotBlank(isJiuquan)) {
            return Result.success(complaintService.getComplaintList(page, limit, keyWord, selectCrtTime, isJiuquan));
        }
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        return Result.success(complaintService.selectComplaintList(page, limit, keyWord, selectCrtTime,user));
    }

    //删除投诉
    @GetMapping("/deleteComplaintList")
    public Result<Object> deleteComplaintList(HttpServletRequest request, Integer id) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        complaintService.deleteComplaintList(id);
        return Result.success("操作成功");
    }

    //办理
    @GetMapping("/adminExamineComplaint")
    public Result<Object> examineComplaint(HttpServletRequest request, Integer id, Integer status, String remark, String handleRemark, String rejectRemark) {
        String token = request.getHeader("authori-zation");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String userId = TokenUtils.verify(token);
        if (userId.equals("false")) {
            return Result.error(405, "token已过期");
        }
        AdminUserModel user = adminUserService.getUserByUserId(Integer.parseInt(userId));
        if (user == null) {
            return Result.error(406, "用户不存在");
        }
        complaintService.examineComplaint(id, status, remark,handleRemark,rejectRemark,user);
        return Result.success("操作成功");
    }

    @GetMapping("/selectHandle")
    public Result<Object> selectHandle(Integer id) {
        return Result.success(complaintService.selectHandle(id));
    }
}
